/*
Source : https://leetcode.com/problems/reverse-integer/
Author : nflush@outlook.com
Date   : 2016-05-11
*/
/*
36. Valid Sudoku  
Total Accepted: 73598 Total Submissions: 239295 Difficulty: Easy

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.


A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. 
*/

int chToIndex(char ch)
{
    switch(ch){
    case '1':
        return 0x1;
    case '2':
        return 0x2;
    case '3':
        return 0x4;
    case '4':
        return 0x8;
    case '5':
        return 0x10;
    case '6':
        return 0x20;
    case '7':
        return 0x40;
    case '8':
        return 0x80;
    case '9':
        return 0x100;
    default:
        return 0;
    }
}

bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
    int rowMask[9]  = {0};
    int colMask[9]  = {0};
    int aeraMask[9] = {0};
    int r,c,index;
    for(r=0;r<boardRowSize;r++){
        for(c=0;c<boardColSize;c++){
            index = chToIndex(board[r][c]);
            if((rowMask[r]|colMask[c]|aeraMask[(r/3)*3+(c/3)])&index){
                return false;
            }
            rowMask[r]|=index;
            colMask[c]|=index;
            aeraMask[(r/3)*3+(c/3)]|=index;
        }
    }
    return true;
}

bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
    int rM[9] = {0};
    int cM[9] = {0};
    int aM[9] = {0};
    int r,c,i;
    for(r=0;r<boardRowSize;r++){
        for(c=0;c<boardColSize;c++){
            i = chToIndex(board[r][c]);
            if(!i) continue;
            if((rM[r]|cM[c]|aM[(r/3)*3+(c/3)])&i){
                return false;
            }
            rM[r]|=i;
            cM[c]|=i;
            aM[(r/3)*3+(c/3)]|=i;
        }
    }
    return true;
}